// JavaScript Document
    //<![CDATA[
    //
// Declaring as global object so can be used by other functions

var map = null;
var markersArray = [];
var markersClustererList = [];

var customIcons = { 
      restaurant: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      },
      markercluster: {
        icon: 'http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/1.0/images/m2.png'
      }
};
 
function bindInfoWindow(marker, map, infoWindow, html) { 
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}
 
function doNothing() {}
 
    //]]> 
 
 
// Deletes all markers in the array by removing references to them 
function deleteOverlays() {
    if(markersClustererList && markersClustererList.length > 0)    
        for(idx in markersClustererList)
            markersClustererList[idx].clearMarkers();        
    
    if (markersArray)
    {
        for (idx in markersArray)
            markersArray[idx].setMap(null);
        markersArray.length = 0;
    }
}

//**************** CUSTOM FUNCTIONS**********************
function initializeMAP_V3(latitude, longitude, zoom, convas)
{
    var latlng = new google.maps.LatLng(latitude, longitude);

    //  SUPPORTED MAP TYPE IDs
    //  1. ROADMAP displays the normal, default 2D tiles of Google Maps
    //  2. SATELLITE displays photographic tiles
    //  3. HYBRID displays a mix of photographic tiles and a tile layer for prominent features (roads, city names).
    //  4. TERRAIN displays physical relief tiles for displaying elevation and water features (mountains, rivers, etc.).
    var myOptions = {
        zoom: zoom,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    map = new google.maps.Map(convas, myOptions);
    
    map.scaleControl=true;
    
    loadMarker(map,"");
    
}

// AUTO LOCATE FUNCTION
function autoLocate(lat, lng, zoom)
{
    if(map)
    {
        map.panTo(new google.maps.LatLng(lat, lng));
        map.setZoom(zoom);
    }
        
}

// Load Markers
function loadMarker(map,para)
{

    var infoWindow = new google.maps.InfoWindow;
    deleteOverlays();
    url = "http://" + location.hostname + "/main/Map_Data/search/"+para;
        
    downloadUrl(url, function(data) {
                var xml = parseXml(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        
        for (var i = 0; i < markers.length; i++) {

            //var name = markers[i].getAttribute("Species");
            var a= "searchResultwithLoaction('"+ markers[i].getAttribute("Location") +"')";
            var location = '<a href="#" onclick="'+ a +'">'+markers[i].getAttribute("Location")+'</a>';
            //var type = markers[i].getAttribute("type");
            //var quantity = markers[i].getAttribute("quantity");
            var point = new google.maps.LatLng(
                        parseFloat(markers[i].getAttribute("lat")),
                        parseFloat(markers[i].getAttribute("lng")));
            var html = "Location:" + location;
            var icon = customIcons["bar"] || {};

            var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon,
                shadow: icon.shadow,
                title:location
            });
            markersArray.push(marker);
            
            bindInfoWindow(marker, map, infoWindow, html);
        }
        var markerCluster = new MarkerClusterer(map, markersArray);
        markersClustererList.push(markerCluster);        
        //markerCluster.clearMarkers();
        
    });
    
}

// Changes the Observation button (Top Nav) background and hides Markers
function updateObservationStatus(btnStatus, imgObservation, enablePic, disablePic)
{
    if(btnStatus)
    {
        if(btnStatus.value == "1")
        {
            document[imgObservation].src = disablePic;
            btnStatus.value = "0";

            deleteOverlays();
        }
        else
        {
            document[imgObservation].src = enablePic;
            btnStatus.value = "1";
            loadMarker(map,"");
        }
    }    
}
function searchResultwithLoaction(str)
{
    $("#searchbox").val(str);
                            	$("#searchresult").html('<img src="/images/loading/loading9.gif">');
				var cValue = str;
				$.post("http://"+location.hostname+"/searchresult/Index", {search: cValue}, function(data) {
                                $("#searchresult").html(data);
                                $("#TotalCount").text(" for: " + $("#searchbox").val());

                                  if($("#accordion").is(":visible")==false)
                                      {

                                        showAccordion();





                                      }
				});

				return false;
}
function updateImage() {
   var baseUrl = "http://maps.google.com/maps/api/staticmap?";

   var params = [];

     params.push("center=" + map.getCenter().lat().toFixed(6) + "," + map.getCenter().lng().toFixed(6));
     var zm= map.getZoom();
     params.push("zoom=" + zm);
     params.push("maptype="+map.mapTypeId);
     var ht=document.getElementById("content").style.height.split("px");
     var wt=document.documentElement.clientWidth;
     params.push("size="+wt+"x"+ht[0]);



    var icon = customIcons["bar"] || {};
   var markersArray1 = [];
   var markerParams = [];
   for (var i = 0; i < markersArray.length; i++) {

       markersArray1.push(markersArray[i].getPosition().lat().toFixed(6) + "," + markersArray[i].getPosition().lng().toFixed(6));
      
       markerParams.push("icon:"+icon.icon);


   }
   

   if (markersArray1.length) {
     var markersString = markerParams.join("|");
     if (markerParams.length) markersString += "|";
     markersString += markersArray1.join("|");
     params.push("markers=" + markersString);
   }


   var img = document.createElement("img");
   img.src = baseUrl + params.join("&") + "&sensor=false";
   saveImageAs(img);

}
function saveImageAs (imgOrURL) {
if (typeof imgOrURL == "object")
imgOrURL = imgOrURL.src;
window.win = open (imgOrURL);
setTimeout("win.document.execCommand('SaveAs')", 500);
}
